package com.lancoo.skegn;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.lancoo.skegn.model.SkegnObj;
import com.tt.CoreType;
import com.tt.QType;
import com.tt.SkEgnManager;
import com.tt.listener.OnInitEngineListener;
import com.tt.listener.OnRecordListener;
import com.tt.setting.EngineSetting;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.UUID;



/**
 * Created by pengmi on 2018/7/31.
 * Description:对SkEgnManager 进行二次封装
 */


public class LancooSkEgnManager {

    public static LancooSkEgnManager lancooSkEgnManager;
    private boolean isShowScore,isEnableScore;
    private OnLancooRecordListener onLancooRecordListener;
    private Context mContext;
    private OnSkEgnInitListener onSkEgnInitListener;


    private LancooSkEgnManager(){

    }

    public static LancooSkEgnManager getInstance(){
        if(lancooSkEgnManager==null){
            lancooSkEgnManager=new LancooSkEgnManager();
        }
        return lancooSkEgnManager;
    }

    //自定义url
    public void startRecord(Context mContext,String content, String url, OnLancooRecordListener onLancooRecordListener){
        this.mContext=mContext;
        this.onLancooRecordListener=onLancooRecordListener;
        isEnableScore=true;
        isShowScore=true;
        SkEgnManager.getInstance(mContext).startRecord(CoreType.EN_SENT_EVAL, content, QType.QTYPE_EMPTY,url,mOnRecordListener);
//        SkEgnManager.getInstance(mContext).startRecord(RequestParamsConstant.CORETYPE,  content, RequestParamsConstant.QTYPE, mHandler);
    }


    public void startRecord(Context mContext,String content, OnLancooRecordListener onLancooRecordListener){
        this.mContext=mContext;
        this.onLancooRecordListener=onLancooRecordListener;
        isEnableScore=true;
        isShowScore=true;
        SkEgnManager.getInstance(mContext).startRecord(CoreType.EN_SENT_EVAL, content, QType.QTYPE_EMPTY,getTempRecordFilePath(UUID.randomUUID()+""), mOnRecordListener);
//        SkEgnManager.getInstance(mContext).startRecord(RequestParamsConstant.CORETYPE, content, RequestParamsConstant.QTYPE, mHandler);
    }

    public void stopRecord(boolean isShowScore){
        this.isShowScore=isShowScore;
        SkEgnManager.getInstance(mContext).stopRecord();
        if(!isShowScore){
            SkEgnManager.getInstance(mContext).cancelRecord();
        }


    }

    public float getVolume(){
        return SkEgnManager.getInstance(mContext).getVolume();
    }


    OnRecordListener mOnRecordListener = new OnRecordListener() {
        @Override
        public void onRecordStart() {
            //开始录制
            if(onLancooRecordListener!=null){
                onLancooRecordListener.onRecordStart();
            }
        }

        @Override
        public void onRecording(int vad_status, int sound_intensity) {
            //录制中
            if(onLancooRecordListener!=null){
                onLancooRecordListener.onRecording(vad_status,sound_intensity);
            }
        }
        @Override
        public void onRecordEnd(String result) {
//            Log.e("aaaa","onRecordEnd:"+result+","+isShowScore+","+isEnableScore);
            if(isEnableScore){
                isEnableScore=false;
                try{
                    if(isShowScore){
                        final JSONObject json = new JSONObject(result);
                        int grade = json.getJSONObject("result").getInt("overall");
                        Gson gson=new Gson();
                        SkegnObj skegnResult = gson.fromJson(result, SkegnObj.class);
                        String path = SkEgnManager.getInstance(mContext).getPath();
                        if(onLancooRecordListener!=null&&!TextUtils.isEmpty(path)){
                            onLancooRecordListener.onRecordEnd(grade,path,skegnResult);
                        }else{
                            onLancooRecordListener.onRecordError("path is empty!");
                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    if(onLancooRecordListener!=null&&isShowScore){
                        onLancooRecordListener.onRecordError(e.toString());
                    }
                }
            }
        }
    };


    public interface OnLancooRecordListener{
        void onRecordStart();
        void onRecording(int vad_status, int sound_intensity);
        void onRecordEnd(int grade, String url, SkegnObj skegnResult);
        void onRecordError(String error);
    }

    public String getTempRecordFilePath(String recorderFileName){
        File targetDir = new File(Environment.getExternalStorageDirectory(),
                "Android/data/" + mContext.getApplicationInfo().packageName + "/files/record/klg");
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
//        File mRecordTempDir=new File(targetDir.getAbsolutePath(),"klg");
//        if(!mRecordTempDir.exists()){
//            mRecordTempDir.mkdir();
//        }
        Log.e("aaa","path:"+targetDir.getPath());
        return targetDir.getPath()+"/"+recorderFileName+".wav";
    }


    public void initSkegn(final Context mContext){
        final EngineSetting setting = EngineSetting.getInstance(mContext);
        setting.setOnInitEngineListener(new OnInitEngineListener() {
            @Override
            public void onStartInitEngine() {
                //开始初始化引擎
                onSkEgnInitListener.onStartInitEngine();
            }

            @Override
            public void onInitEngineSuccess() {
                //初始化引擎成功
                Log.e("aaaaaaa","打分引擎初始化成功!");
                onSkEgnInitListener.onInitEngineSuccess();
            }

            @Override
            public void onInitEngineFailed() {
                //初始化引擎失败
//                isInitSkEgn=true;
                onSkEgnInitListener.onInitEngineFailed();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SkEgnManager.getInstance(mContext).initCloudEngine("148757611600000f","2d5356fe1d5f3f13eba43ca48c176647","userId", setting);
            }
        }).start();
    }

    public  void initSkegn(final Context mContext, final OnSkEgnInitListener onSkEgnInitListener){
        this.onSkEgnInitListener=onSkEgnInitListener;
        initSkegn(mContext);


    }


    public interface OnSkEgnInitListener {

        void onStartInitEngine();//引擎开始初始化

        void onInitEngineSuccess();//引擎初始化成功

        void onInitEngineFailed();//引擎初始化失败

    }

}
